package com.polaris.live.adapter.live

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LifecycleCoroutineScope
import com.chad.library.adapter.base.BaseQuickAdapter
import com.polaris.live.R
import com.polaris.live.databinding.LiveMessageBackpackGiftItemBinding
import com.polaris.live.databinding.LiveMessageGiftItemBinding
import com.polaris.live.databinding.LiveMessageInviteCallItemBinding
import com.polaris.live.databinding.LiveMessageInviteFollowItemBinding
import com.polaris.live.databinding.LiveMessageInviteGiftItemBinding
import com.polaris.live.databinding.LiveMessageInviteSeatItemBinding
import com.polaris.live.databinding.LiveMessageNewFollowerBinding
import com.polaris.live.databinding.LiveMessageNotHavaBinding
import com.polaris.live.databinding.LiveMessagePublicItemBinding
import com.polaris.live.databinding.LiveMessageRecommendCardItemBinding
import com.polaris.live.databinding.LiveMessageTipsItemBinding
import com.polaris.live.im.sfs.LIVE_INVITE_CALL
import com.polaris.live.im.sfs.LIVE_INVITE_FOLLOW
import com.polaris.live.im.sfs.LIVE_INVITE_GIFT
import com.polaris.live.im.sfs.LIVE_NEW_FOLLOWER
import com.polaris.live.im.sfs.LIVE_NOT_HAVE
import com.polaris.live.im.sfs.LIVE_PROMPT
import com.polaris.live.im.sfs.LIVE_PUBLIC_SCREEN
import com.polaris.live.im.sfs.LIVE_RECOMMEND_CARD
import com.polaris.live.im.sfs.LIVE_SEND_GIFT
import com.polaris.live.im.sfs.LIVE_UP_SEAT_INVITE
import com.polaris.live.im.sfs.LUCKY_BACKPACK_GIFT
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.ui.live.widget.adapter.LiveMessageBackPackGiftItem
import com.polaris.live.ui.live.widget.adapter.LiveMessageGiftItem
import com.polaris.live.ui.live.widget.adapter.LiveMessageInviteCallItem
import com.polaris.live.ui.live.widget.adapter.LiveMessageInviteFollowerItem
import com.polaris.live.ui.live.widget.adapter.LiveMessageInviteGiftItem
import com.polaris.live.ui.live.widget.adapter.LiveMessageNewFollowerItem
import com.polaris.live.ui.live.widget.adapter.LiveMessageNotHavaItem
import com.polaris.live.ui.live.widget.adapter.LiveMessagePublicItem
import com.polaris.live.ui.live.widget.adapter.LiveMessageRecommendCardItem
import com.polaris.live.ui.live.widget.adapter.LiveMessageTipsItem
import com.polaris.live.ui.live.widget.adapter.LiveMessageUpSeatItem
import com.polaris.live.ui.live.widget.adapter.view_holder.LiveMessageViewHolder

/**
 * 直播通话公屏消息
 *
 * @author Created by 半仙 on 2023/10/24/024 14:25
 */
class LiveMessageAdapter(
    private val lifecycleScope: () -> LifecycleCoroutineScope?
) : BaseQuickAdapter<SendLiveMessage, LiveMessageViewHolder>(0) {

    init {
        addChildClickViewIds(
            R.id.call_follower_ad_im,
            R.id.invite_to_call,
            R.id.call_gift_gift,
            R.id.call_up_seat_text,
            R.id.call_up_seat_im,
            R.id.tips_ll
        )
    }

    override fun getDefItemViewType(position: Int): Int {
        return convertItemType(data[position].type)
    }

    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): LiveMessageViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            LIVE_PUBLIC_SCREEN -> LiveMessagePublicItem(
                this,
                LiveMessagePublicItemBinding.inflate(layoutInflater, parent, false),
                lifecycleScope()
            )

            LIVE_PROMPT -> LiveMessageTipsItem(
                this,
                LiveMessageTipsItemBinding.inflate(layoutInflater, parent, false)
            )

            LIVE_SEND_GIFT -> LiveMessageGiftItem(
                this,
                LiveMessageGiftItemBinding.inflate(layoutInflater, parent, false),
                lifecycleScope()
            )

            LIVE_INVITE_FOLLOW -> LiveMessageInviteFollowerItem(
                this,
                LiveMessageInviteFollowItemBinding.inflate(layoutInflater, parent, false)
            )

            LIVE_INVITE_GIFT -> LiveMessageInviteGiftItem(
                this,
                LiveMessageInviteGiftItemBinding.inflate(layoutInflater, parent, false)
            )

            LIVE_INVITE_CALL -> LiveMessageInviteCallItem(
                this,
                LiveMessageInviteCallItemBinding.inflate(layoutInflater, parent, false)
            )

            LIVE_NEW_FOLLOWER -> LiveMessageNewFollowerItem(
                this,
                LiveMessageNewFollowerBinding.inflate(layoutInflater, parent, false),
                lifecycleScope()
            )

            LIVE_UP_SEAT_INVITE -> LiveMessageUpSeatItem(
                this,
                LiveMessageInviteSeatItemBinding.inflate(layoutInflater, parent, false)
            )

            LUCKY_BACKPACK_GIFT -> LiveMessageBackPackGiftItem(
                this,
                LiveMessageBackpackGiftItemBinding.inflate(layoutInflater, parent, false),
                lifecycleScope()
            )

            LIVE_RECOMMEND_CARD -> LiveMessageRecommendCardItem(
                this,
                LiveMessageRecommendCardItemBinding.inflate(layoutInflater, parent, false),
                lifecycleScope()
            )

            else -> LiveMessageNotHavaItem(
                this,
                LiveMessageNotHavaBinding.inflate(layoutInflater, parent, false)
            )
        }
    }

    override fun convert(holder: LiveMessageViewHolder, item: SendLiveMessage) {
        when (holder) {
            is LiveMessagePublicItem -> holder.bind(item)
            is LiveMessageTipsItem -> holder.bind(item)
            is LiveMessageGiftItem -> holder.bind(item)
            is LiveMessageInviteFollowerItem -> holder.bind(item)
            is LiveMessageInviteGiftItem -> holder.bind(item)
            is LiveMessageInviteCallItem -> holder.bind(item)
            is LiveMessageNotHavaItem -> holder.bind(item)
            is LiveMessageNewFollowerItem -> holder.bind(item)
            is LiveMessageUpSeatItem -> holder.bind(item)
            is LiveMessageBackPackGiftItem -> holder.bind(item)
            is LiveMessageRecommendCardItem -> holder.bind(item)
        }
    }


    companion object {

        fun convertItemType(itemType: Int): Int {
            return when (itemType) {
                LIVE_PUBLIC_SCREEN -> LIVE_PUBLIC_SCREEN        //公屏消息
                LIVE_PROMPT -> LIVE_PROMPT                      //提示消息
                LIVE_SEND_GIFT -> LIVE_SEND_GIFT                //礼物消息
                LUCKY_BACKPACK_GIFT -> LUCKY_BACKPACK_GIFT      //背包幸运礼物消息
                LIVE_INVITE_FOLLOW -> LIVE_INVITE_FOLLOW        //邀请关注消息
                LIVE_INVITE_GIFT -> LIVE_INVITE_GIFT            //邀请送礼
                LIVE_INVITE_CALL -> LIVE_INVITE_CALL            //邀请通话
                LIVE_NEW_FOLLOWER -> LIVE_NEW_FOLLOWER          //用户提示消息（关注）
                LIVE_UP_SEAT_INVITE -> LIVE_UP_SEAT_INVITE      //邀请上座信息
                LIVE_RECOMMEND_CARD -> LIVE_RECOMMEND_CARD      //推荐卡片消息
                else -> LIVE_NOT_HAVE                           //用户版本不同的 老版本没有该类型就展示没有
            }
        }
    }

}